package com.baizhi.cmfz.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.baizhi.cmfz.entity.Admin;
import com.baizhi.cmfz.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;


	/**
	 *
	 * @param session session作用域，同于存放一些数据
	 * @param response 响应对象，用于向浏览器相应结果
	 * @throws IOException 抛出异常
	 */
	@RequestMapping("/getCode")
	public void getCode(HttpSession session,HttpServletResponse response) throws IOException {
//		利用工具类生成验证码图片
		CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(160, 60, 5, 20);

//		获取验证码的文字内容
		String code = captcha.getCode();

//		将验证码的内容放入session中
		System.out.println(code);
		session.setAttribute("code",code);

//		输出到浏览器
		captcha.write(response.getOutputStream());
	}



	@RequestMapping("/adminLogin")
	public String adminLogin(String username,String password,String code,HttpSession session){
//		session中获取验证码内容
		String code1 = (String) session.getAttribute("code");
		/*if(code1.equalsIgnoreCase(code)){
			Admin admin = adminService.selectAdminByNameAndPassword(username, password);
			if(admin!=null){
				System.out.println("登陆成功");
				session.setAttribute("admin",admin);
				return "main";
			}
		}*/
//		将数据封装到UsernamePasswordToken中
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//		执行验证
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
//			System.err.println("断点");
			return "main";
		} catch (Exception e) {
			e.printStackTrace();
			return "login";
		}
	}

	/**
	 *
	 * @param page 分页查询的页码
	 * @param limit 每页显示的行数
	 * @return 返回所有数据的集合
	 */
	@RequestMapping("/selectAdminPage")
	@ResponseBody
	public Map selectAdminPage(int page,int limit){
		return adminService.selectAdminPage(page,limit);
	}


	/**
	 *
	 * @param admin 要添加到数据库中的信息
	 * @return 返回添加之后的详细报告
	 */
	@RequestMapping("/insertAdmin")
	@ResponseBody
	public Map insertAdmin(Admin admin){
		return adminService.insertAdmin(admin);
	}


	/**
	 *
	 * @param admin  根据对象中的数据对数据库中的数据进行修改
	 * @return 返回修改的详细信息
	 */
	@RequiresPermissions(value = {"role:update"})
	@RequestMapping("/updateAdmin")
	@ResponseBody
	public Map updateAdmin(Admin admin){
		return adminService.updateAdmin(admin);
	}


	/**
	 *
	 * @param adminId 要删除的数据的id值
	 * @return 返回删除的详细信息
	 */
	@RequiresPermissions(value = {"role:delete"})
	@RequestMapping("/deleteAdmin")
	@ResponseBody
	public Map deleteAdmin(int adminId){
		return adminService.deleteAdmin(adminId);
	}


	/**
	 *
	 * @param adminId 根据id查询信息
	 * @return 返回这个id的admin的实例
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	public Admin selectById(int adminId){
		return adminService.selectById(adminId);
	}

}
